package com.huanwei.mainfragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.huanwei.cc.R;
import com.huanwei.questionfragment.DHD_Fragment;
import com.huanwei.questionfragment.YHD_Fragment;
import com.huanwei.cc.ui.BaseViewPagerAdapter;
import com.huanwei.cc.utils.SizeUtils;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * : 论坛
 */
public class Forum_Fragment extends Fragment {

    String[] mTitles = {"已回答", "待回答" };
    List<Fragment> mFragmentList = new ArrayList<>(mTitles.length);
    @BindView(R.id.mi_main)
    MagicIndicator miMain;
    @BindView(R.id.vp_main)
    ViewPager vpMain;
    View view;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = View.inflate(getActivity(), R.layout.fragment_main, null);
            ButterKnife.bind(this, view);
            initViewsAndEvents();
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }

        return view;
    }




    public void initViewsAndEvents() {
        CommonNavigator commonNavigator = new CommonNavigator(getContext());
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mTitles.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView
                        (context);
                simplePagerTitleView.setNormalColor(Color.parseColor("#333333"));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color
                        .colorPrimaryDark));
                simplePagerTitleView.setText(mTitles[index]);
                simplePagerTitleView.setTextSize(16);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        vpMain.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.colorPrimaryDark));
                indicator.setLineHeight(SizeUtils.dp2px(getActivity(), 2));
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                return indicator;
            }

            @Override
            public float getTitleWeight(Context context, int index) {
                return 1.0f;
            }
        });
        miMain.setNavigator(commonNavigator);
        mFragmentList.add(YHD_Fragment.newInstance());
        mFragmentList.add(DHD_Fragment.newInstance());
//        mFragmentList.add(DSH_Fragment.newInstance());
//        mFragmentList.add(YSH_Fragment.newInstance());

        vpMain.setOffscreenPageLimit(mTitles.length);
        vpMain.setAdapter(new BaseViewPagerAdapter(getChildFragmentManager(), mFragmentList));
        ViewPagerHelper.bind(miMain, vpMain);
    }

}
